const { readJSONSync, writeFileSync } = require('fs-extra');
const prettier = require('prettier');
const { get: objectGet, set: objectSet, isBoolean } = require('lodash');
const path = require('path');

const root = path.resolve(__dirname, '../');
  const resolve = (src) => path.resolve(root, src);
const sleep = (time) => {
  return new Promise((resolve) => {
    setTimeout(resolve, time);
  });
};
const ls = {
    get(key) {
      const data = readJSONSync(resolve('./config.secret.json'));
      return objectGet(data, key);
    },
    set(key, value) {
      const data = readJSONSync(resolve('./config.secret.json'));
      const originValue = objectGet(data, key);
      let ret;
      if (value === '!' && isBoolean(originValue)) {
        objectSet(data, key, !originValue);
        ret = !originValue;
      } else {
        objectSet(data, key, value);
        ret = value;
      }
      writeFileSync(
        resolve('./config.secret.json'),
        prettier.format(JSON.stringify(data), {
          parser: 'json'
        })
      );
      return ret;
    }
  };
module.exports = {
  sleep,
  ls,
  root
};
